package routers

import (
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"go-gin-init/app/middleware"
	"go-gin-init/docs"
	"go.uber.org/zap"
)

type RouterGroup struct {
	*gin.RouterGroup
}

func NewRouter() *gin.Engine {
	// 禁止警告
	gin.SetMode("release")
	ginRouter := gin.New()
	ginRouter.Use(middleware.GinLogger(), middleware.GinRecovery(true))
	ginRouter.Use(middleware.Cors())

	//ginRouter.Use(middleware.JWTAuth())
	registerSwagger(ginRouter)

	apiRouterGroup := ginRouter.Group("api")

	routerGroup := RouterGroup{apiRouterGroup}

	routerGroup.UserRouter()
	routerGroup.PostRouter()

	return ginRouter
}

func registerSwagger(r gin.IRouter) {
	// API文档访问地址: http://host/swagger/index.html
	// 注解定义可参考 https://github.com/swaggo/swag#declarative-comments-format
	// 样例 https://github.com/swaggo/swag/blob/master/example/basic/api/api.go
	docs.SwaggerInfo.Title = "gin-init 接口"
	docs.SwaggerInfo.BasePath = "/api"
	docs.SwaggerInfo.Description = "gin-init 初始化项目"
	docs.SwaggerInfo.Version = "1.0"
	docs.SwaggerInfo.Host = "localhost:9000"
	docs.SwaggerInfo.Schemes = []string{"http", "https"}
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	zap.L().Info("swagger 在线api文档地址: http:127.0.0.1:9000/swagger/index.html")
}
